package com.luoye.lj.wstool.wsdm;

import com.luoye.lj.wstool.common.StringUtils;
import com.luoye.lj.wstool.generator.ICodeGenerator;
import com.luoye.lj.wstool.wsdm.entity.WsdtEntity;
import com.luoye.lj.wstool.wsdm.entity.WsdtField;

/*
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 2016/4/26
 * Time: 15:43
 * To change this template use File | Settings | File Templates.
 */
public class FieldsCodeGenerator implements ICodeGenerator {
    private WsdtEntity wsdtEntity;

    public FieldsCodeGenerator(WsdtEntity wsdtEntity) {
        this.wsdtEntity = wsdtEntity;
    }

    @Override
    public String codeGenerate() {
        String code="";
        String fieldsCode="";
        String getterSetterCode="";
        if(this.wsdtEntity!=null){
            if(this.wsdtEntity.getFieldList()!=null){
                for(WsdtField wsdtField:this.wsdtEntity.getFieldList()){
                    if("int".equals(wsdtField.getFieldType())||"float".equals(wsdtField.getFieldType())||"double".equals(wsdtField.getFieldType())||"short".equals(wsdtField.getFieldType())
                            ||"long".equals(wsdtField.getFieldType())||"byte".equals(wsdtField.getFieldType())){
                        fieldsCode += "    private " + wsdtField.getFieldType() + " " + wsdtField.getFieldName() + "=-1;//" + wsdtField.getDescription() + "\n";
                    }else {
                        fieldsCode += "    private " + wsdtField.getFieldType() + " " + wsdtField.getFieldName() + ";//" + wsdtField.getDescription() + "\n";
                    }
                    if("boolean".equals(wsdtField.getFieldType())||"Boolean".equals(wsdtField.getFieldType())) {
                        getterSetterCode += "    public " + wsdtField.getFieldType() + " is" + StringUtils.makeFirstCharUpcase(wsdtField.getFieldName()) + "() {\n" +
                                "        return " + wsdtField.getFieldName() + ";\n" +
                                "    }\n" +
                                "\n" +
                                "    public void set" + StringUtils.makeFirstCharUpcase(wsdtField.getFieldName()) + "(" + wsdtField.getFieldType() + " " + wsdtField.getFieldName() + ") {\n" +
                                "        this." + wsdtField.getFieldName() + " = " + wsdtField.getFieldName() + ";\n" +
                                "    }\n" +
                                "\n";
                    }else{
                        getterSetterCode += "    public " + wsdtField.getFieldType() + " get" + StringUtils.makeFirstCharUpcase(wsdtField.getFieldName()) + "() {\n" +
                                "        return " + wsdtField.getFieldName() + ";\n" +
                                "    }\n" +
                                "\n" +
                                "    public void set" + StringUtils.makeFirstCharUpcase(wsdtField.getFieldName()) + "(" + wsdtField.getFieldType() + " " + wsdtField.getFieldName() + ") {\n" +
                                "        this." + wsdtField.getFieldName() + " = " + wsdtField.getFieldName() + ";\n" +
                                "    }\n" +
                                "\n";
                    }
                }
            }
        }
        code=fieldsCode+"\n"+getterSetterCode;
        return code;
    }
}
